<template>
  <div class='m-sys_indicators'>
    <a-table :data='data' :cols='tableKeys'>
      <el-table-column align="center" label="操作" width='180' slot='ahead'>
        <template slot-scope="scope">
          <div>
            <el-button size='mini' @click='choose(scope.row)'>设置指标</el-button>
          </div>
        </template>
      </el-table-column>
    </a-table>
    <div class='u-pagination'>
      <!-- 分页 -->
      <pagination ref='page' @change='getData' :action='fetchData' :pageSize='20'></pagination>
    </div>
    <!-- dialog -->
    <el-dialog
      title="绩效指标设置"
      :visible.sync="dialogVisible"
      :before-close="() => { this.dialogVisible = false }"
    >
      <el-form class='dialog_form' :inline='true' label-width="120px">
        <el-form-item label='员工名称'>{{ detail.name }}</el-form-item>
        <el-form-item label='进取业绩指标'>
          <el-input type='number' v-model.trim='detail.aggressive' />
        </el-form-item>
        <el-form-item label='基础业绩指标'>
          <el-input type='number' v-model.trim='detail.base' />
        </el-form-item>
        <div style='width: 100%;text-align: center;'>
          <el-button type='primary' @click='confirm'>确定</el-button>
          <el-button @click='dialogVisible = false'>取消</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'm-sys_indicators',

  data() {
    return {
      tableKeys: [
        { prop: 'login_name', label: '登录账号', minWidth: '100' },
        { prop: 'name', label: '名称', minWidth: '100' },
        { prop: 'base', label: '基础业绩指标', minWidth: '100' },
        { prop: 'aggressive', label: '进取业绩指标', minWidth: '100' }
      ],
      data: [],
      dialogVisible: false,
      selectId: '',
      detail: {
        name: '',
        base: 0,
        aggressive: 0
      },
      ifRequesting: false
    }
  },

  mounted() {
    this.$refs.page.changePage()
  },

  methods: {
    async fetchData(aPageNum, aPageSize) {
      return this.$api.linkGetStaffKpis({
        aFirmId: this.aFirmId,
        aPageNum,
        aPageSize
      })
    },

    getData(data) {
      this.data = data
    },

    choose({ staff_id, base, aggressive, name }) {
      this.detail.name = name
      this.detail.base = base
      this.detail.aggressive = aggressive
      this.selectId = staff_id
      this.dialogVisible = true
    },

    async confirm() {
      const { base, aggressive } = this.detail
      const { code } = await this.$api.linkSetStaffKpiq({
        aStaffId: this.selectId,
        aBaseKpi: base,
        aAgrgressiveKpi: aggressive
      })
      if (code === 0) {
        this.__notify('提示', '修改成功')
        this.$refs.page.changePage()
        this.dialogVisible = false
      }
    }
  }
}
</script>

<style lang='less'>
.m-sys_indicators {
  .dialog_form {
    .el-form-item, .el-form-item__content, .el-input {
      width: 100%;
    }
    .el-form-item {
      display: flex;
    }
  }
}
</style>